<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="地名" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入地名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收件人" prop="nick">
        <el-input
          v-model="queryParams.nick"
          placeholder="请输入收件人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="地址ID" prop="addrId">
        <el-input
          v-model="queryParams.addrId"
          placeholder="请输入地址ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="所属地区" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入所属地区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小区" prop="comy">
        <el-input
          v-model="queryParams.comy"
          placeholder="请输入小区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="门牌号" prop="door">
        <el-input
          v-model="queryParams.door"
          placeholder="请输入门牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="详细地址" prop="detail">
        <el-input
          v-model="queryParams.detail"
          placeholder="请输入详细地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="默认地址" prop="isFirst">
        <el-input
          v-model="queryParams.isFirst"
          placeholder="请输入默认地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="已删除" prop="deleted">
        <el-input
          v-model="queryParams.deleted"
          placeholder="请输入已删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['taskhub:userAddr:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['taskhub:userAddr:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['taskhub:userAddr:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['taskhub:userAddr:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="userAddrList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="               " align="center" prop="id" />
      <el-table-column label="地名" align="center" prop="title" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="收件人" align="center" prop="nick" />
      <el-table-column label="联系号码" align="center" prop="phone" />
      <el-table-column label="地址ID" align="center" prop="addrId" />
      <el-table-column label="所属地区" align="center" prop="area" />
      <el-table-column label="定位地址" align="center" prop="location" />
      <el-table-column label="小区" align="center" prop="comy" />
      <el-table-column label="楼栋" align="center" prop="ridgepole" />
      <el-table-column label="单元 " align="center" prop="unit" />
      <el-table-column label="门牌号" align="center" prop="door" />
      <el-table-column label="详细地址" align="center" prop="detail" />
      <!-- <el-table-column label="经度" align="center" prop="x" /> -->
      <!-- <el-table-column label="纬度" align="center" prop="y" /> -->
      <el-table-column label="默认地址" align="center" prop="isFirst" />
      <!-- <el-table-column label="性别" align="center" prop="sex" /> -->
      <!-- <el-table-column label="已删除" align="center" prop="deleted" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['taskhub:userAddr:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['taskhub:userAddr:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户地址对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="地名" prop="title">
          <el-input v-model="form.title" placeholder="请输入地名" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="收件人" prop="nick">
          <el-input v-model="form.nick" placeholder="请输入收件人" />
        </el-form-item>
        <el-form-item label="联系号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系号码" />
        </el-form-item>
        <el-form-item label="地址ID" prop="addrId">
          <el-input v-model="form.addrId" placeholder="请输入地址ID" />
        </el-form-item>
        <el-form-item label="所属地区" prop="area">
          <el-input v-model="form.area" placeholder="请输入所属地区" />
        </el-form-item>
        <el-form-item label="定位地址" prop="location">
          <el-input v-model="form.location" placeholder="请输入定位地址" />
        </el-form-item>
        <el-form-item label="小区" prop="comy">
          <el-input v-model="form.comy" placeholder="请输入小区" />
        </el-form-item>
        <el-form-item label="楼栋" prop="ridgepole">
          <el-input v-model="form.ridgepole" placeholder="请输入楼栋" />
        </el-form-item>
        <el-form-item label="单元 " prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单元 " />
        </el-form-item>
        <el-form-item label="门牌号" prop="door">
          <el-input v-model="form.door" placeholder="请输入门牌号" />
        </el-form-item>
        <el-form-item label="详细地址" prop="detail">
          <el-input v-model="form.detail" placeholder="请输入详细地址" />
        </el-form-item>
        <!-- <el-form-item label="经度" prop="x">
          <el-input v-model="form.x" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="y">
          <el-input v-model="form.y" placeholder="请输入纬度" />
        </el-form-item> -->
        <el-form-item label="默认地址" prop="isFirst">
          <el-input v-model="form.isFirst" placeholder="请输入默认地址" />
        </el-form-item>
        <!-- <el-form-item label="已删除" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入已删除" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserAddr, getUserAddr, delUserAddr, addUserAddr, updateUserAddr } from '@/api/taskhub/userAddr'

export default {
  name: 'UserAddr',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户地址表格数据
      userAddrList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        userId: null,
        nick: null,
        phone: null,
        addrId: null,
        area: null,
        comy: null,
        door: null,
        detail: null,
        isFirst: null,
        sex: null,
        deleted: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: '地名不能为空', trigger: 'blur' }
        ],
        userId: [
          { required: true, message: '用户ID不能为空', trigger: 'blur' }
        ],
        nick: [
          { required: true, message: '收件人不能为空', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '联系号码不能为空', trigger: 'blur' }
        ],
        addrId: [
          { required: true, message: '地址ID不能为空', trigger: 'blur' }
        ],
        area: [
          { required: true, message: '所属地区不能为空', trigger: 'blur' }
        ],
        comy: [
          { required: true, message: '小区不能为空', trigger: 'blur' }
        ],
        ridgepole: [
          { required: true, message: '楼栋不能为空', trigger: 'blur' }
        ],
        door: [
          { required: true, message: '门牌号不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 获取参数
    const userId = this.$route.query.userId
    if (userId) {
      this.queryParams.userId = userId
    }
    this.getList()
  },
  methods: {
    /** 查询用户地址列表 */
    getList() {
      this.loading = true
      listUserAddr(this.queryParams).then(response => {
        this.userAddrList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        userId: null,
        nick: null,
        phone: null,
        addrId: null,
        area: null,
        location: null,
        comy: null,
        ridgepole: null,
        unit: null,
        door: null,
        detail: null,
        x: null,
        y: null,
        isFirst: null,
        sex: null,
        deleted: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加用户地址'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getUserAddr(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改用户地址'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUserAddr(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addUserAddr(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除用户地址编号为"' + ids + '"的数据项？').then(function() {
        return delUserAddr(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('taskhub/userAddr/export', {
        ...this.queryParams
      }, `userAddr_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
